/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Action;
import model.entityTable;
import sql.AConnection;

/**
 *
 * @author aragon
 */
public class AppBorrowRoom extends AConnection implements entityTable<AppBorrowRoom> {

    private String idABR;
    private User user;
    private ArrayList<String> listIdDSTGM;
    private ArrayList<String> listIdCTPhong;
    private ArrayList<DetailRoomE> rooms;
    private String date_cens;
    private String date_regis;
    private String date_THM;
    private String content;
    private String require;
    private String censorship;
    private String hide;

    public AppBorrowRoom(String idABR, User user, ArrayList<String> listIdDSTGM, ArrayList<String> listIdCTPhong, ArrayList<DetailRoomE> rooms, String date_cens, String date_regis, String date_THM, String content, String require, String censorship, String hide) {
        this.idABR = idABR;
        this.user = user;
        this.listIdDSTGM = listIdDSTGM;
        this.listIdCTPhong = listIdCTPhong;
        this.rooms = rooms;
        this.date_cens = date_cens;
        this.date_regis = date_regis;
        this.date_THM = date_THM;
        this.content = content;
        this.require = require;
        this.censorship = censorship;
        this.hide = hide;
    }

    
    public AppBorrowRoom() {
    }
    

    public boolean select() {
        try {
            String query = "SELECT idUser,LyDo,Duyet FROM donmuonphong WHERE idDon=?  and AnHien=?";
            String query1 = "SELECT idDSTGM,idCTPhong FROM dstgmuon WHERE idDon=?";
            Connection connection = getConnection();
            //------------
            PreparedStatement ps = connection.prepareStatement(query);
            ps.setString(1, getIdABR());
            ps.setString(2, getHide());
            ResultSet rs = ps.executeQuery();
            //------------

            user = new User();
            if (rs.next()) {
                user.setIdUser(rs.getString("idUser"));
                setContent(rs.getString("LyDo"));
                setCensorship(rs.getString("Duyet"));
                if (!user.select()) {
                    return false;
                }
            }
            //------------
            PreparedStatement ps1 = connection.prepareStatement(query1);
            ps1.setString(1, idABR);
            ResultSet rs1 = ps1.executeQuery();
            listIdDSTGM = new ArrayList<String>();
            listIdCTPhong = new ArrayList<String>();
            //------------
            int i = 0;
            while (rs1.next()) {
                i++;
                listIdDSTGM.add(rs1.getString("idDSTGM"));
                listIdCTPhong.add(rs1.getString("idCTPhong"));
            }
            if (i == 0) {
                return false;
            }
            rooms = new ArrayList<DetailRoomE>();
            for (String in : listIdCTPhong) {
                DetailRoomE dr = new DetailRoomE();
                dr.setIdDRM(in);
                dr.select();
                rooms.add(dr);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public ResultSet selectDB() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean insert() {
        try {
            String query = "INSERT INTO quanlyphonghoc.donmuonphong (idDon, idUser, LyDo,YeuCau,NgayTHM, NgayMuon, NgayDuyet, Duyet, AnHien) "
                    + "VALUES (NULL, ?, ?,?, ?, ?,'0000-00-00', '0', '1')";
            Connection connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(query);
            ps.setString(1,getUser().getIdUser() );
            ps.setString(2,getContent() );
            ps.setString(3,getRequire() );
            ps.setString(4,Action.converDatetoMysql(getDate_THM()));
            ps.setString(5,Action.converDatetoMysql(getDate_regis()));
            ps.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;
        }

    }

    public boolean update() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean remove() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ResultSet selectAll(int hide) {
        
        try {
            String query = "SELECT  idDon  FROM  donmuonphong  WHERE  AnHien  = ? order by NgayMuon asc";
            Connection connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(query);
            ps.setString(1, hide + "");
            ResultSet rs = ps.executeQuery();
            
            return rs;
        } catch (SQLException ex) {
            return null;
        }

    }
    public String selectIdABR(){
        try {
            String query = "SELECT idDon FROM donmuonphong WHERE idUser=? and LyDo=? and YeuCau= ? and NgayTHM=? and NgayMuon=?";
            Connection connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(query);
            ps.setString(1,getUser().getIdUser() );
            ps.setString(2,getContent() );
            ps.setString(3,getRequire() );
            ps.setString(4,Action.converDatetoMysql(getDate_THM()) );
            ps.setString(5,Action.converDatetoMysql(getDate_regis()) );
            ResultSet rs= ps.executeQuery();
            if(rs.next()){
             return rs.getString("idDon");
            }else{
                return null;
            }
        } catch (SQLException ex) {
            return null;
        }
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getHide() {
        return hide;
    }

    public void setHide(String hide) {
        this.hide = hide;
    }

    public String getIdABR() {
        return idABR;
    }

    public void setIdABR(String idABR) {
        this.idABR = idABR;
    }

    public ArrayList<DetailRoomE> getRooms() {
        return rooms;
    }

    public void setRooms(ArrayList<DetailRoomE> rooms) {
        this.rooms = rooms;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getCensorship() {
        return censorship;
    }

    public void setCensorship(String censorship) {
        this.censorship = censorship;
    }

    public ArrayList<String> getListIdCTPhong() {
        return listIdCTPhong;
    }

    public void setListIdCTPhong(ArrayList<String> listIdCTPhong) {
        this.listIdCTPhong = listIdCTPhong;
    }

    public ArrayList<String> getListIdDSTGM() {
        return listIdDSTGM;
    }

    public void setListIdDSTGM(ArrayList<String> listIdDSTGM) {
        this.listIdDSTGM = listIdDSTGM;
    }

    public String getRequire() {
        return require;
    }

    public void setRequire(String require) {
        this.require = require;
    }

    public String getDate_cens() {
        return date_cens;
    }

    public void setDate_cens(String date_cens) {
        this.date_cens = date_cens;
    }

    public String getDate_regis() {
        return date_regis;
    }

    public void setDate_regis(String date_regis) {
        this.date_regis = date_regis;
    }

    public String getDate_THM() {
        return date_THM;
    }

    public void setDate_THM(String date_THM) {
        this.date_THM = date_THM;
    }
    

}
